package com.projecy.system.controller;


import com.projecy.common.exception.BadRequestException;
import com.projecy.common.utils.ResultUtil;
import com.projecy.common.utils.SecurityUtil;
import com.projecy.logs.annotation.Log;
import com.projecy.system.model.SysMenu;
import com.projecy.system.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequiredArgsConstructor
@RequestMapping("/sys")
public class SysMenuController extends ResultUtil {
    @Autowired
    SysMenuService menuService;
    @Log("获取菜单树")
    @GetMapping("/menu/tree")
    public ResponseEntity<Object> getMenuTree(){

        try {
            List<String> roles = SecurityUtil.getCurrentRoles();
            System.out.println("获取菜单树/menu/tree"+ menuService.getMenuTree(roles));
            return success(true, menuService.getMenuTree(roles));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }


    @RequestMapping("/menu/anomaly")
    public ResponseEntity<Object> Anomaly(){


            return fail(false, "没有权限");

    }

    @Log("获取左侧菜单树")
    @GetMapping("/menu/tree/left")
    public ResponseEntity<Object> getMenuTreeForLeft(){
        System.out.println("获取左侧菜单树");
        try {
            List<String> roles = SecurityUtil.getCurrentRoles();
            return success(true, menuService.getMenuTreeForLeft(roles));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }



    @Log("获取下拉框里面的树")
    @GetMapping("/menu/select/tree")
    public ResponseEntity<Object> getMenuSelectTree(String type){
        System.out.println("获取下拉框里面的树");
        try {
            return success(true, menuService.getMenuSelectTree(type));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("获取菜单列表")
    @GetMapping("/menu/table")
    public ResponseEntity<Object> getMenuTable(String blurry){
        System.out.println("获取菜单列表");
        try {
            return success(true, menuService.getMenuTable(blurry));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    //查询当前用户所有权限菜单
    @GetMapping("/menu/all")
    public ResponseEntity<Object> queryAllMenus(){
        System.out.println("查询当前用户所有权限菜单");
        try {

            List<String> roles = SecurityUtil.getCurrentRoles();
            System.out.println("rolesss"+roles);
            //System.out.println(" menuService.queryAllMenus(roles)"+ menuService.queryAllMenus(roles));
            return success(true, menuService.queryAllMenus(roles));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("编辑菜单")
    @PostMapping("/menu/edit")
    public ResponseEntity<Object> editMenu(@RequestBody SysMenu sysMenu){
        try {
            menuService.editMenu(sysMenu);
            return success(true, sysMenu.getId() == null ? "添加成功" : "修改成功");
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("删除菜单")
    @DeleteMapping("/menu/F")
    public ResponseEntity<Object> delMenu(Long id){
        try {
            menuService.delMenu(id);
            return success(true, "删除成功");
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

    @Log("获取角色菜单树")
    @GetMapping("/menu/role/tree")
    public ResponseEntity<Object> getMenuTreeByRoleId(String roleId){
        try {
            return success(true, menuService.getMenuTreeByRoleId(roleId));
        } catch (BadRequestException e) {
            e.printStackTrace();
            return fail(false, e.getMsg());
        }
    }

}
